package com.rdgzs.base.utils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import net.sf.json.JSONArray;
import net.sf.json.JsonConfig;

import org.apache.commons.lang.StringUtils;

import com.rdgzs.base.entity.dict.SysDictionary;
import com.rdgzs.framework.constant.SysConstants;
import com.rdgzs.framework.entity.DictDataVO;
import com.rdgzs.framework.utilsTools.Pinyin4jUtil;
import com.rdgzs.framework.utilsTools.UtilsObject;

public class DictionaryUtils {
	public static Map<String, List<SysDictionary>> dictMap = new HashMap();
	
	public static List<SysDictionary> get(String key) throws Exception {
		if (StringUtils.isNotBlank(key)) {
			return dictMap.get(key);
		} else {
			throw new Exception("数据字典查询时,给定的key参数为 null 或 空 ！");
		}
	}
	
	public static String getParamNameByKey(String dictKey, String parameKey) {
		String name = "";
		if (parameKey != null && dictKey != null) {
			List<SysDictionary> dictDatas = dictMap.get(dictKey.trim());
			for (SysDictionary data : dictDatas) {
				if (parameKey.trim().equals(data.getParamCode())) {
					name = data.getParamName();
				}
			}
		}
		return name;
	}
	
	public static boolean isEmpty() {
		if (dictMap != null && dictMap.isEmpty()) {
			return false;
		} else {
			return true;
		}
	}
	
	public static Set<String> keys() {
		return dictMap.keySet();
	}
	
	public static int size() {
		return dictMap.size();
	}
	
	public static String getParamCode(String key, String ParameterName) throws Exception {
		String result = "";
		List<SysDictionary> dataList = DictionaryUtils.get(key);
		for (SysDictionary o : dataList) {
			if (ParameterName.equals(o.getParamName())) {
				result = o.getParamCode();
				break;
			}
		}
		return result;
	}
	
	public static String getParamName(String key, String ParameterCode) throws Exception {
		String result = "";
		List<SysDictionary> dataList = DictionaryUtils.get(key);
		for (SysDictionary o : dataList) {
			if (ParameterCode.equals(o.getParamCode())) {
				result = o.getParamName();
				break;
			}
		}
		return result;
	}
	
	public static String getMoreParameterName(String key, String ParameterCode, String suffix) throws Exception {
		String result = "";
		List<SysDictionary> dataList = DictionaryUtils.get(key);
		for (SysDictionary o : dataList) {
			if (ParameterCode.indexOf(o.getParamCode()) >= 0) {
				result += o.getParamName() + suffix;
			}
		}
		return result;
	}
	
	/**
	 * 自动取得数据字典
	 * 
	 * @param dictTypeCode
	 * @throws Exception
	 */
	public static String getDictList(String dictTypeCode) throws Exception {
		List<SysDictionary> list = DictionaryUtils.get(dictTypeCode);
		String[] field_excludes = new String[6];
		field_excludes[0] = "optimistic";
		field_excludes[1] = "createTime";
		field_excludes[2] = "modifyTime";
		field_excludes[3] = "createId";
		field_excludes[4] = "modifyId";
		field_excludes[5] = "dictType";
		JsonConfig jsonConfig_excludes = UtilsObject.Obj2JsonConfig_excludes(field_excludes);
		JSONArray qualificationFields = JSONArray.fromObject(list, jsonConfig_excludes);
		return qualificationFields.toString();
	}
	
	/**
	 * 获取页面自动补全的的代码表信息
	 * 
	 * @param typeCode
	 *            代码表类型
	 * @param q
	 *            页面自动补全输入框的值
	 * @return
	 * @throws Exception
	 */
	public static List getDataListForAutoComplete(String typeCode, String q) throws Exception {
		List retList = new ArrayList();
		try {
			List<SysDictionary> inquiryTypeList = DictionaryUtils.get(typeCode);
			for (SysDictionary dictData : inquiryTypeList) {
				String parameterCode = dictData.getParamCode();
				String parameterName = dictData.getParamName();
				String[] parameterPinyins = Pinyin4jUtil.makeStringByStringSet(Pinyin4jUtil.getPinyin(parameterName))
						.split(SysConstants.SEPARATOR_COMMA);
				
				for (String parameterPinyin : parameterPinyins) {
					if (parameterName.startsWith(q) || parameterPinyin.startsWith(q)) {
						DictDataVO dictDataVO = new DictDataVO();
						dictDataVO.setTypeCode(typeCode);
						dictDataVO.setParameterCode(parameterCode);
						dictDataVO.setParameterName(parameterName);
						if (!retList.contains(dictDataVO)) {
							retList.add(dictDataVO);
						}
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		}
		return retList;
	}
	
}
